using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using CodePlex.Resourceful;

namespace CodePlex.SpaceBlock.UI.Controls
{
    public partial class DataGridView2 : DataGridView, ISupportDialogKeys
    {
        public event EventHandler<DialogKeyEventArgs> ProcessingDialogKey;

        private Dictionary<int, string[]> _suggestions = new Dictionary<int, string[]>();

        public DataGridView2()
        {
            InitializeComponent();
            this.Setup();
        }

        private void Setup()
        {
            this.AllowUserToAddRows = false;
            this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.RowHeadersVisible = false;
            this.AllowUserToResizeRows = false;
            this.AllowUserToDeleteRows = false;

            this.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            this.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(0xf8,0xf8,0xf8);
            this.AdvancedCellBorderStyle.All = DataGridViewAdvancedCellBorderStyle.None;


            DataGridViewColumn colImage = new DataGridViewImageColumn();
            colImage.Name = "Image";
            colImage.HeaderText = "";
            colImage.Width = 18;

            this.Columns.Add(colImage);

            this.SelectionChanged += delegate
            {

                // this should not really be here...
                IEnumerable<long> sizes = Utils.ConvertAll<DataGridViewRow,long>(this.SelectedRows, delegate(DataGridViewRow item)
                {
                    IFileItem fi = item.Tag as IFileItem;
                    return fi!=null?fi.Size:0;
                });

                long totalSize = Utils.Aggregate<long,long>(sizes, delegate(long item0, long item1)
                {
                    return item0 + item1;
                });

                string sizeString = totalSize == 0 ? "" : string.Format(" ({0})", Utils.ComputeSizeString(totalSize));

                UIManager.Instance.SetStatus("{0} object{1} selected{2}.", 
                    this.SelectedRows.Count,
                    this.SelectedRows.Count==1?"":"s",
                    sizeString);


            };

            this.MouseDown += delegate(object sender, MouseEventArgs e)
            {
             

                // force select on right-mouse-down
                if (e.Button == MouseButtons.Right)
                {
                    HitTestInfo info = this.HitTest(e.X, e.Y);
                    if (info.RowIndex > -1 && !this.Rows[info.RowIndex].Selected)
                    {
                        this.ClearSelection();
                        this.SetSelectedRowCore(info.RowIndex, true);
                    }
                }

               
                // if left-click 
                if (e.Button == MouseButtons.Left)
                {
                    HitTestInfo info = this.HitTest(e.X, e.Y);
                    if (info.RowIndex > -1 && this.SelectedRows.Count > 1 && this.Rows[info.RowIndex].Selected)
                    {
                 //       putback = this.SelectedRows;
                    }
                }

               
            };

            this.EditingControlShowing += delegate(object sender, DataGridViewEditingControlShowingEventArgs e)
            {
                DataGridViewTextBoxEditingControl text = e.Control as DataGridViewTextBoxEditingControl;


                if (text != null)
                {
                    text.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                    text.AutoCompleteSource = AutoCompleteSource.CustomSource;
                    text.AutoCompleteCustomSource.AddRange(_suggestions[this.CurrentCell.ColumnIndex]);
                }

                DataGridViewComboBoxEditingControl combo = e.Control as DataGridViewComboBoxEditingControl;

               
                if (combo != null)
                {
                    combo.DropDownStyle = ComboBoxStyle.DropDown;
                   
                }
            };

        }

        public DataGridViewTextBoxColumn AddColumnText(string name, int width, params string[] suggest)
        {

            DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
            
            col.Name = name;
            col.Width = width;
            col.ReadOnly = suggest.Length == 0;
            this.Columns.Add(col);

            _suggestions.Add(col.Index, suggest);
            return col;
        }

        public DataGridViewComboBoxColumn AddColumnCombo(string name, int width, params string[] suggest)
        {

            DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();


            col.DataSource = suggest;

            col.Name = name;
            col.Width = width;
            //col.CellTemplate.FormattedValueType = typeof(string);
            col.ReadOnly = suggest.Length == 0;
            this.Columns.Add(col);

            _suggestions.Add(col.Index, suggest);
            return col;
        }


        public void AddRow(object tag, params object[] values)
        {

            DataGridViewRow row = (DataGridViewRow)this.RowTemplate.Clone();

            row.CreateCells(this, values);
            row.Tag = tag;
            row.Height = 18;
            row.ReadOnly = false;

            this.Rows.Add(row);

        }


        protected override bool ProcessDataGridViewKey(KeyEventArgs e)
        {
            if (e.KeyData == Keys.Delete)
            {
                if (ProcessingDialogKey != null)
                {
                    DialogKeyEventArgs args = new DialogKeyEventArgs(e.KeyData);

                    ProcessingDialogKey(this, args);

                }
            }
            return base.ProcessDataGridViewKey(e);
        }
       
     
    }
}
